/*test5_2.c ：售票模拟系统*/
#include <stdio.h>
#include <pthread.h>
int ticket[10] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
int rest = 10;

void *terminal(int *num)   /*终端线程共享代码*/
{
    int tn = *num;    /*num所指单元里存放的是终端线程编号*/
    int tot = 0;
    long i;
    while (1)
        if (rest > 0)
        {
            rest--;
            tot++;
            for (i = 0; i < 999999; i++);
        }
        else
            break;
    printf("Selled %d tickets from terminal_%d\n", tot, tn);
    pthread_exit(0);
}

int main()
{
    pthread_t tid[2];
    int num[2] = {1, 2}; /*num为终端线程编号*/
    /*<创建售票终端线程1和售票终端线程2 >*/
    int i;
    for(i=0; i<2; i++)
    {
        int *n=&num[i];
        pthread_create(&tid[i], NULL, (void *)(* terminal), n);
    }
    /*<等待线程的终止>*/
    pthread_join(tid[0],NULL);
    pthread_join(tid[1],NULL);
    printf("Over!\n");
    return 0;
}
